package com.yunzhong.appointment.system.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yunzhong.appointment.config.Const;
import com.yunzhong.appointment.entity.Department;
import com.yunzhong.appointment.entity.Departmenttype;
import com.yunzhong.appointment.mapper.DepartmentMapper;
import com.yunzhong.appointment.mapper.DepartmenttypeMapper;
import com.yunzhong.appointment.mapper.PersonMapper;
import com.yunzhong.appointment.mapper.ProfessionalMapper;
import com.yunzhong.appointment.system.service.DepartmentService;
import com.yunzhong.appointment.util.PageData;

/**
 * 
 * @className DepartmentServiceImpl
 * @description 部门模型层接口实现类
 * @author 石洪刚
 * @time 2017年11月14日 下午4:42:48
 */
@Service
public class DepartmentServiceImpl implements DepartmentService{

	@Autowired
	private DepartmentMapper departmentMapper;
	@Autowired
	private DepartmenttypeMapper departmentTypeMapper;
	@Autowired
	private PersonMapper personMapper;
	@Autowired
	private ProfessionalMapper professionalMapper;
	@Override
	public List<Department> listDepartment(PageData pd) {
		//如果是通过页面选项卡发送的请求，需要得到上次的页号
		if(StringUtils.isNotBlank(pd.getString("lastPage1"))){
			pd.put(Const.PAGE_NUM, pd.getString("lastPage1"));
		}
		pd.setPageData(pd);
		return departmentMapper.listDepartment(pd);
	}

	@Override
	public List<Departmenttype> listDepartmentType(PageData pd) {
		if(StringUtils.isNotBlank(pd.getString("lastPage2"))){
			pd.put(Const.PAGE_NUM, pd.getString("lastPage2"));
		}
		pd.setPageData(pd);
		return departmentTypeMapper.listDepartmentType(pd);
	}

	@Override
	public Department getDepartmentById(String id) {
		return departmentMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Departmenttype> listDepartmentType() {
		return departmentTypeMapper.listDepartmentType(null);
	}

	@Override
	public Departmenttype getDepartmentTypeById(String id) {
		return departmentTypeMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdateDepartment(Department department) {
		if("".equals(department.getDpId())){
			department.setDpId(UUID.randomUUID().toString());
			departmentMapper.insertSelective(department);
		}else{
			departmentMapper.updateByPrimaryKeySelective(department);
		}
	}

	@Override
	public void saveOrUpdateDepartmentType(Departmenttype departmentType) {
		if("".equals(departmentType.getDplId())){
			departmentType.setDplId(UUID.randomUUID().toString());
			departmentTypeMapper.insertSelective(departmentType);
		}else{
			departmentTypeMapper.updateByPrimaryKeySelective(departmentType);
		}
		
	}

	@Override
	public void removeDepartment(String[] ids) {
		for (String id : ids) {
			//设置人员中相关科室和职称数据为null
			personMapper.updateDpNullByDpId(id);
			//删除对应职称
			professionalMapper.deleteByDpId(id);
			//删除科室
			departmentMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public void removeDepartmentType(String[] ids) {
		for (String id : ids) {
			//得到科室类型下所有科室并处理
			List<Department> deptList = departmentMapper.listDeptByDplId(id);
			//删除科室类型
			for (Department department : deptList) {
				this.removeDepartment(new String[]{department.getDpId()});
			}
			departmentTypeMapper.deleteByPrimaryKey(id);
		}
	}

}
